package goover.unit03;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;

public class HashSetDemo02 {
    public static void main(String[] args){
        HashSet hst = new HashSet();
        Student stu1 = new Student(1,"zhangsan");
        Student stu2 = new Student(1,"zhangsan");
        Student stu3 = new Student(2,"lisi");
        hst.add(stu1);
        hst.add(stu2);
        hst.add(stu3);
        System.out.println(hst);
        Iterator iterator = hst.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
class Student{
    int id;
    String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return id == student.id &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}